if !exists(param.S)
  
  M291 S5 J1 F250 L150 H450 R"Set Left Tool Temperature" P"Please set the temperature for the filament previously loaded in the Left Tool."
  var ll = input
  var rr = 0
  
  G10 P0 R{var.ll} S{var.ll}
  G10 P1 R{var.rr} S{var.rr}
  G10 P2 R{var.ll,var.rr} S{var.ll,var.rr}
  G10 P3 R{var.ll,var.rr} S{var.ll,var.rr}
  
  M568 P0 A2
  M568 P1 A2
  
  M291 S2 R"Please Remove the Build Sheet" P"To avoid damaging your machine, remove the build sheet from the build plate.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop." S2
  
  if !move.axes[0].homed || !move.axes[1].homed || !move.axes[2].homed || !move.axes[3].homed
    M98 P"0:/sys/homeall.g" S1
  
  G90
  G1 X-999 U999 Y-75 F18000
  
  M116 P0 S10
  M116 P1 S10
  
  T0
  M83                 ; relative extruder moves
  G1 E-50 F{60}*{10}   ; retract filament
  
  M98 P"0:/sys/nozzlewipe.g" C1
  M400
  while iterations < 2
    M98 P"0:/sys/nozzlewipe.g" L1 C1
    M400
  
  G1 F18000 X-50 Y-150 Z125  ; Move tools to the cleaning position
  M291 S2 R"Please Clean Both Nozzles" P"Use a metal brush to clean both nozzles thoroughly to prevent machine damage.<br>Click ""OK"" when you are ready to proceed or ""Cancel"" to stop." S2
  
  M98 P"0:/sys/homeall.g" S1
  
  G29 S2     ; disable Mesh Bed Leveling
  M290 R0 S0
  M208 Z-5 S1
  M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
  M98 P"0:/user/probeoffset.g"                       ; load global variables
  T0
  G1 U999 F18000
  M42 P4 S1                                              ; Turn on relay





















;====Probe with Nozzle====

G90
G1 F18000 X0 Y80 Z5 F18000

G91
M98 P"0:/sys/nozzleprobe.g" Z1
var zn = move.axes[2].machinePosition
G1 Z20






;====Probe with Z - Probe====
M98 R1 P"0:/sys/attachedcheck.g" ; make sure probe is conected, pick if negative and leave relay active
M558 K0 P8 C"1.io4.in" H5 F100 T18000              ; define Z probe parameters
M98 P"0:/user/probeoffset.g"                       ; load global variables

G90                 ; absolute positioning
G1 X{0 - sensors.probes[0].offsets[0]} Y{80 - sensors.probes[0].offsets[1]} F18000

G91
G38.2 K0 Z0 ; Probe Z
G4 P260
var zp1 = move.axes[2].machinePosition
G1 Z5

G38.2 K0 Z0 ; Probe Z
G4 P260
var zp2 = move.axes[2].machinePosition
G1 Z5

G38.2 K0 Z0 ; Probe Z
G4 P260
var zp3 = move.axes[2].machinePosition
G1 Z5


var zp =  (var.zp1 + var.zp2 + var.zp3)/3


var dd = var.zp - var.zn
echo "New Z offset is: "^{var.dd}^" mm"


;======= Aditional offset ======= Increase (+) to lower, Decrease (-) to raise

set global.zoffset = var.dd - {0.0}  ; Apply babystepping to zoffset (add for less Z offset)

;======= Aditional offset =======


echo >"0:/user/probeoffset.g" "; Set Z probe trigger value, offset and trigger height"
echo >>"0:/user/probeoffset.g" "G31 P500 X0 Y28.50 Z"^{global.zoffset}
M98 P"0:/user/probeoffset.g"                       ; load global variables

echo >"0:/user/zoffset.g" "if exists(global.zoffset)"
echo >>"0:/user/zoffset.g" "  set global.zoffset = "^{global.zoffset}
echo >>"0:/user/zoffset.g" "else"
echo >>"0:/user/zoffset.g" "  global zoffset = "^{global.zoffset}

G30           ; Home bed using probe to update datum


























if !exists(param.S)

  G10 P0 S0 R0
  G10 P1 S0 R0
  G10 P2 S0 R0
  G10 P3 S0 R0
  
  M208 Z0 S1
  M558 K0 P8 C"1.io4.in" H5 F300 T18000
  M98 P"0:/user/probeoffset.g"                       ; load global variables
  
  G90
  G1 X-999 U999 Y150 Z100 F18000  